package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Organization;
import com.company.project.service.OrganizationService;
import com.company.project.service.impl.OrganizationServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Resource
    private OrganizationServiceImpl organizationService;

    /**
     * 查询机构介绍
     * @return
     */
    @RequestMapping(value = "/selectOrg",method = RequestMethod.GET)
    @ResponseBody
    public  List<Organization>  selectOrg(){
        List<Organization> organizations = organizationService.selectOrganization();
        return organizations;
    }

    /**
     * 修改机构介绍
     * @return
     */
    @PostMapping("/updateOrganization")
    @ResponseBody
    public Result updateOrganization(@RequestBody Organization organization){
        Boolean aBoolean = organizationService.updateOrganization(organization);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 添加机构介绍
     * @param organization
     * @return
     */
    @PostMapping("/insertO")
    @ResponseBody
    public Result insertO(@RequestBody Organization organization){
        organizationService.insertO(organization);
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/add")
    public Result add(Organization organization) {
        organizationService.save(organization);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        organizationService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Organization organization) {
        organizationService.update(organization);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Organization organization = organizationService.findById(id);
        return ResultGenerator.genSuccessResult(organization);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Organization> list = organizationService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
